package com.anuo.app.modules.coach.controller;

import com.anuo.app.common.basedata.BusinessType;
import com.anuo.app.common.globalhandle.BaseController;
import com.anuo.app.common.basedata.AuditState;
import com.anuo.app.common.entity.Request;
//import com.anuo.app.common.persistence.RedisUtils;
import com.anuo.app.common.entity.RowState;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.utils.Assert;
import com.anuo.app.common.utils.DateUtil;
import com.anuo.app.modules.coach.entity.Coach;
import com.anuo.app.modules.coach.entity.CoachCourse;
import com.anuo.app.modules.coach.entity.CoachFilter;
import com.anuo.app.modules.coach.service.CoachService;
import com.anuo.app.modules.system.service.FileService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;

@RestController
public class CoachController extends BaseController {
    @Autowired
    CoachService coachService;
    @Autowired
    FileService fileService;

    @PostMapping(value = "v1/apiSaveCoachApply", produces = {"application/json;charset=UTF-8"})
    public String apiSaveCoachApply(@RequestBody String encryptedReq) {
        Coach coachReq = getRequestObject(encryptedReq, new TypeReference<Request<Coach>>() {
        }).getFirstEntity();//反序列化取得对象

        //验证数据
        Coach coachObj = coachService.getCoachById(coachReq.getId());
        if (coachObj != null && AuditState.PROCESSING.equals(coachReq.getState())) {
            return rspError("审核中的教练不能再申请");
        }
        //删除已拒绝的教练申请
        coachService.deleteRefuesCoachApply(coachReq.getId());
        //保存用户
        coachService.saveCoach(coachReq);
        //保存图片信息
        fileService.saveFileinfoList(coachReq.getFileinfoList(), BusinessType.COACH, coachReq.getId());
        return rspSuccess();
    }

    @PostMapping(value = "v1/apiGetCoachApply", produces = {"application/json;charset=UTF-8"})
    public String apiGetCoachApply(@RequestBody String encryptedReq) {
        CoachFilter coachFilter = getRequestObject(encryptedReq, new TypeReference<Request<CoachFilter>>() {
        }).getFirstEntity();//反序列化取得对象
        return rspSuccess(coachService.getCoachApply(coachFilter));
    }

    @PostMapping(value = "v1/apiGetCoach", produces = {"application/json;charset=UTF-8"})
    public String apiGetCoach(@RequestBody String encryptedReq) {
        CoachFilter coachFilter = getRequestObject(encryptedReq, new TypeReference<Request<CoachFilter>>() {
        }).getFirstEntity();//反序列化取得对象

        boolean isHaveAuditInfo = coachService.isHaveAuditInfo(coachFilter);
        //获取教练评分
        //BigDecimal coachScore = coachService.getCoachAVGScore(coachFilter);
        Coach coach = coachService.getCoach(coachFilter);
        //coach.setScore(coachScore);
        //计算教龄
        if (coach.getBegin_teaching_date() != null) {
            int beginTeachYear = DateUtil
                    .getCalendar(coach.getBegin_teaching_date())
                    .get(Calendar.YEAR);
            int coachAge = DateUtil.getCurrentYearInt() - beginTeachYear;
            coach.setCoach_age(coachAge);
        }
        coach.setCurrentuser_iscoach(coachService.isExistCoachInfo(coach.getId()));
        //获取教练的单价
        if (StringUtils.isNotEmpty(coachFilter.getCityid())) {

        } else {
            return rspError("教练单价不存在");
        }

        if (coach.getRowState() == RowState.ADDED) {
            //去新增
        }

        return rspSuccess(coach);
    }

    @PostMapping(value = "v1/apiGetCoachSyllabus", produces = {"application/json;charset=UTF-8"})
    public String apiGetCoachSyllabus(@RequestBody String encryptedReq) {
        CoachFilter coachFilter = getRequestObject(encryptedReq, new TypeReference<Request<CoachFilter>>() {
        }).getFirstEntity();//反序列化并获取entities中首个对象

        List<CoachCourse> coachCourseList = coachService.getCoachSyllabus(coachFilter);
        for (CoachCourse coachCourse : coachCourseList) {
            coachCourse.setCourse_maxposition(1);
        }
        return rspSuccess(coachCourseList);
    }

    /**
     * 查询附近的教练
     * (如果需要测试性能, 可以去查询shop_area 这张表, 里面有很多的地理数据)
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiGetNearbyCoachList", produces = {"application/json;charset=UTF-8"})
    public String apiGetNearbyCoachList(@RequestBody String encryptedReq) {
        CoachFilter coachFilter = getRequestObject(encryptedReq, new TypeReference<Request<CoachFilter>>() {
        }).getFirstEntity();//反序列化并获取entities中首个对象

        PageData pageData = coachService.getNearbyCoachList(coachFilter);
        return rspSuccess(pageData);
    }

    @PostMapping(value = "v1/apiGetCoachList", produces = {"application/json;charset=UTF-8"})
    public String apiGetCoachList(@RequestBody String encryptedReq) {
        CoachFilter coachFilter = getRequestObject(encryptedReq, new TypeReference<Request<CoachFilter>>() {
        }).getFirstEntity();//反序列化并获取entities中首个对象

        PageData pageData = coachService.getCoachList(coachFilter);
        return rspSuccess(pageData);
    }

    @PostMapping(value = "v1/apiGetCoachDetail", produces = {"application/json;charset=UTF-8"})
    public String apiGetCoachDetail(@RequestBody String encryptedReq) {
        CoachFilter coachFilter = getRequestObject(encryptedReq, new TypeReference<Request<CoachFilter>>() {
        }).getFirstEntity();//反序列化并获取entities中首个对象

        return rspSuccess(coachService.getCoachDetail(coachFilter));
    }

    @PostMapping(value = "v1/apiGetCoachStudentMoney", produces = {"application/json;charset=UTF-8"})
    public String apiGetCoachStudentMoney(@RequestBody String encryptedReq) {
        CoachFilter coachFilter = getRequestObject(encryptedReq, new TypeReference<Request<CoachFilter>>() {
        }).getFirstEntity();//反序列化并获取entities中首个对象
        Assert.isEmpty(coachFilter.getCoachStudentmoney_id(),"coachStudentmoney_id 不能为空");
        return rspSuccess(coachService.getCoachStudentMoney(coachFilter));
    }




}
